<?php
class Order extends AppModel {
        static $PAYMENT_TYPES = array('check' => '小切手','cc' => 'クレジットカード','po' => '購入注文');
        var $displayField = 'name';
        var $validate = array(
                'name' => array(
                        'notempty' => array('rule' => 'notempty','message' => '名前を入力してください'),
                ),
                'email' => array(
                        'notempty' => array('rule' => 'notempty','message' => 'メールアドレスを入力してください','last'=>true),
                        'email' => array('rule' => 'email','message' => 'メールアドレスが不正です'),
                ),
                'address' => array(
                        'notempty' => array('rule' => array('notempty'),'message' => '住所を入力してください'),
                ),
                'pay_type' => array(
                        'notempty' => array('rule' => array('notempty'),'message' => '支払い方法を指定してください'),
                ),
        );
        //The Associations below have been created with all possible keys, those that are not needed can be removed

        var $hasMany = array(
                'LineItem' => array(
                        'className' => 'LineItem',
                        'foreignKey' => 'order_id',
                        'dependent' => false
                )
        );
		var $hasAndBelongsToMany = array(
			'Products' =>
				array(
					'className' => 'Products',
					'joinTable' => 'line_items',
					'foreignKey' => 'order_id',
					'associationForeignKey' => 'product_id',
				)
		);
		function beforeSave() {
			#debug($this->data);
			$this->log('Order#beforeSave() called!!');
			if(array_key_exists('to_be_shipped',$this->data['Order']) && $this->data['Order']['to_be_shipped'] == 1){
				// shipped_at に現時刻をセット
				$this->data['Order']['shipped_at'] =  date('Y-m-d H:i:s');
				return true;
			}else{
				return true;
			}
		}
		
		function pending_shipping(){
			return $this->findAllByShippedAt(NULL);
		}
		function count(){
			return $this->find('count');
		}
		function count_pending(){
			return $this->find('count', array('conditions' => array('Order.shipped_at' => null)));
		}
}
?>